﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Zanox.Api.Client
{
    public static class Utils
    {
        /// <summary>
        ///  "otto de" => "otto"
        /// </summary>     
        public static string CleanShopName(string name)
        {
            int length = name.Length;
            if (length > 3)
            {
                if (name.Substring(length - 3, 1).Equals(" "))
                {
                    return name.Substring(0, length - 3);
                }
                else if (name.Substring(length - 3, 1).Equals("."))
                {
                    return name.Substring(0, length - 3);
                }
            }

            return UppercaseFirst(name);
        }

        /// <summary>
        /// Returns input string with first letter in upper case.
        /// </summary>        
        public static string UppercaseFirst(string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return string.Empty;
            }
            char[] a = s.ToCharArray();
            a[0] = char.ToUpper(a[0]);
            return new string(a);
        }

        /// <summary>
        /// Extracts the partnercode from given ppc trackinglink
        /// </summary>
        /// <param name="trackingLink">ppc trackinglink</param>
        /// <returns>partnercode, e.g.  '1234C5678'</returns>
        public static string GetPartnerCode(string trackingLink)
        {
            return trackingLink.Split("?&".ToCharArray())[1];
        }
    }
}
